<template>
  <div class="container">
    <div class="app-container">
      <el-tree
        :data="depts"
        :props="defaultProps"
        default-expand-all
      >
        <template #default="{data}">
          <el-row style="width:100%;height: 40px;" type="flex" justify="space-between" align="middle">
            <el-col>{{ data.name }}</el-col>
            <el-col :span="4">
              <span style="margin-right: 10px;">{{ data.managerName }}</span>
              <el-dropdown trigger="click" @command="operateDept($event,data.id)">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item icon="el-icon-plus" command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item icon="el-icon-circle-plus" command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item icon="el-icon-circle-plus-outline" command="cle">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown></el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <show-dialog ref="show" :show-dialog.sync="showDialog" :parent-id="parentId" @addDeparent="getDepartmentList" />
  </div>
</template>

<script>
import { getDepartmentList, deleteDepartment } from '@/api/department'
import { transList } from '@/utils/index'
import showDialog from '@/views/department/showDialog.vue'
export default {
  components: {
    showDialog
  },
  data() {
    return {
    // 模拟的部门列表数据
      // 模拟的部门列表数据
      depts: [
        // {
        //   name: '传智教育',
        //   managerName: '张三',
        //   children: [
        //     { name: '总裁办', managerName: '李四' },
        //     { name: '行政部', managerName: '王五' },
        //     {
        //       name: '人事部',
        //       managerName: '张杰',
        //       children: [
        //         { name: '财务核算部', managerName: '杨礼力' },
        //         { name: '税务管理部', managerName: '李佳伟' },
        //         { name: '薪资管理部', managerName: '张国伟' }
        //       ]
        //     }
        //   ]
        // }
      ],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDialog: false,
      parentId: null
    }
  },
  created() {
    this.getDepartmentList()
  },
  methods: {
    async getDepartmentList() {
      const data = await getDepartmentList()
      const newData = transList(data)
      console.log(newData)
      this.depts = newData
    },
    operateDept(command, parentId) {
      if (command === 'add') {
        this.showDialog = true
        this.parentId = parentId
      } else if (command === 'edit') {
        this.showDialog = true
        this.parentId = parentId
        this.$nextTick(() => {
          this.$refs.show.edit()
        })
      } else {
        this.$confirm('毁灭吧', {
          confirmButtonText: '确认',
          cancelButtonText: '取消'
        }).then(async() => {
          await deleteDepartment(parentId)
          this.$message.success('删除部门成功')
          this.getDepartmentList()
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
